package com.qfedu.lc.server.controller;


import com.qfedu.lc.common.param.UserVipAdd;
import com.qfedu.lc.common.util.JwtUtil;
import com.qfedu.lc.common.vo.R;
import com.qfedu.lc.server.service.UserVipService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 8.用户收费会员表 前端控制器
 * </p>
 *
 * @author wgz
 * @since 2023-11-06
 */
@RestController
@RequestMapping("/server/userVip")
public class UserVipController {


    @Resource
    private UserVipService userVipService;

    /**
     * 用户购买vip 或者 vip 续期
     * @param userVipAdd
     * @return
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserVipAdd userVipAdd, HttpServletRequest request){


        if (userVipAdd==null){
            return R.fail("请求参数错");
        }

        //userId
        String token = request.getHeader("Lctoken");
        //解析token
        Integer userId =Integer.valueOf(JwtUtil.parseToken(token,"id").toString());


        // money
        Integer vipType = userVipAdd.getVipType();
        int money =0;
        switch (vipType){
            case 1:
                money=365;
                break;
            case 2:
                money=998;
                break;
            case 3:
                money=10000;
        }

        userVipAdd.setUserId(userId);
        userVipAdd.setMoney(money);

        return userVipService.buyVip(userVipAdd);
    }


}

